package com.kiln.juxingqs.ui

import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.BankManagerAdapter
import com.kiln.juxingqs.databinding.ActivityBankmanagerBinding
import com.kiln.juxingqs.ui.vm.BankManagerViewModel

/**
 * 银行卡管理
 */
class BankManagerActivity : BaseMiddleActivity<BankManagerViewModel, ActivityBankmanagerBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_bankmanager
    }

    val adapter: BankManagerAdapter by lazy {
        BankManagerAdapter(this@BankManagerActivity).apply {
            itemClick = {
                startActivity(Intent(this@BankManagerActivity, BankDetailActivity::class.java).apply {
                    putExtra("bean", it)
                })

//                if (intent.getBooleanExtra("select", false)) {
//                    setResult(Activity.RESULT_OK, Intent().apply {
//                        putExtra("address", it)
//                    })
//                    finish()
//                }
            }
//            defItem = {
////                viewModel.defAddr(it)
//            }
        }
    }

    override fun initViewModel(): BankManagerViewModel = createViewModel()

    override fun initView() {
        binding?.apply {
            event = this@BankManagerActivity
            recyclerView.layoutManager = LinearLayoutManager(this@BankManagerActivity)
            recyclerView.adapter = adapter
            emptyLayout.setOnClickListener { viewModel.holdCall() }
            refreshLayout.setOnRefreshListener {
                if (!viewModel.isLoading.value!!) {
                    viewModel.holdCall()
                } else {
                    refreshLayout.isRefreshing = false
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.holdCall()

    }

    override fun observerUI() {
        viewModel.isLoading.observe(this, Observer {
            if (!it)
                binding.refreshLayout.isRefreshing = false
        })
        viewModel.cardListRet.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    data?.let {
                        adapter.submitList(it.card_list)
                        if (it.card_list != null && it.card_list.size > 0) {
                            binding.emptyLayout.visibility = View.GONE
                        } else {
                            binding.emptyLayout.errorType = (EmptyLayout.NODATA)
                        }
                    }
                } else {
                    if (adapter.itemCount == 0)
                        binding.emptyLayout.errorType = (EmptyLayout.NETWORK_ERROR)
                }
            }
        })
//        viewModel.delRet.observe(this, Observer {
//            it?.apply {
//                if (isSuccess) {
//                    toast("删除成功")
//                    viewModel.call()
//                }
//            }
//        })
//        viewModel.defRet.observe(this, Observer {
//            it?.apply {
//                if (isSuccess) {
//                    toast("设置成功")
//                    viewModel.call()
//                }
//            }
//        })
    }

    fun addBank() {
        startActivity(Intent(this@BankManagerActivity, BankDetailActivity::class.java).apply {
//                    putExtra("bean", it)
//                    putExtra("isEdit", true)
        })
    }

}